<?php

class HTech_Image
{
    protected $_image;
    protected $_type;
    protected $_width;
    protected $_height;
    protected $_aspect;
    protected $_stamp;
    protected $_stampSx = 0;
    protected $_stampSy = 0;

    public function __construct($image) 
    {
        list($this->_width, $this->_height, $this->_type) = getimagesize($image);
        switch ($this->_type) {
            case IMAGETYPE_GIF: 
                $this->_image = imagecreatefromgif($image); break;
            case IMAGETYPE_JPEG: 
                $this->_image = imagecreatefromjpeg($image); break;
            case IMAGETYPE_PNG: 
                $this->_image = imagecreatefrompng($image); break;
        }
        if ($this->_image === false) return false; //throw an exception
        $this->_aspect = $this->_width / $this->_height;   
    }
    
    public function setStamp($stamp)
    {
        $this->_stamp = $stamp;
        return $this;
    }
    
    public function setStampSx($sx)
    {
        $this->_stampSx = $sx;
        return $this;
    }
    
    public function setStampSy($sy)
    {
        $this->_stampSy = $sy;
        return $this;
    }
    
    public function render() 
    {            
        if($this->_stamp) {
            $stamp = imagecreatefrompng($this->_stamp);
            $sx = imagesx($stamp);
            $sy = imagesy($stamp);
            imagecopy($this->_image, $stamp, imagesx($this->_image) - $sx - $this->_stampSx, 
                    imagesy($this->_image) - $sy - $this->_stampSy, 0, 0, imagesx($stamp), imagesy($stamp));
        }
        header('Content-type: image/png');
        imagepng($this->_image);
        imagedestroy($this->_image);        
    }

    
}